﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.WebNemo.Web.Controls;
using mojoPortal.Web.Editor;
using mojoPortal.Web;
using System.Web.UI;
using blackhouse.WebNemo.Web;
using System.Web.UI.WebControls;
using System.Configuration;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;

namespace blackhouse.WebNemo.Cms.Nemo.Controls {
    public class HtmlEditControl : EditRecordValue {

        public override bool DisplayInline {
            get { return false; }
        }

        private TextBox EditorControl { get; set; }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
        }

        protected override void PopulateControl() {
            this.PopulateControlCKEditor();
        }

        private void PopulateControlTinyMCE() {
            SiteSettings site = CacheHelper.GetCurrentSiteSettings();
            List<ContentStyle> contentStyles = ContentStyle.GetAll(site.SiteGuid);
            List<string> bContentStyles = new List<string>();
            foreach (ContentStyle contentStyle in contentStyles.Where(cs => cs.IsActive)) {
                bContentStyles.Add(contentStyle.Name + "=" + contentStyle.CssClass);
            }

            // Dodanie stylu z strony do edytora
            if (Page.Header.FindControl("mojoStyle") == null) {
                Literal lStyle = new Literal();
                lStyle.Text = "<link rel='stylesheet' type='text/css' href='/csshandler.ashx?skin=" + site.Skin + "&sv=" + Guid.NewGuid() + "' />";
                lStyle.ID = "mojoStyle";
                Page.Header.Controls.Add(lStyle);
            }

            this.EditorControl = new TextBox();
            this.EditorControl.ID = "EditControl" + column.ColumnId;
            this.Controls.Add(this.EditorControl);
            this.EditorControl.TextMode = TextBoxMode.MultiLine;
            this.EditorControl.Text = this.record.GetString(this.column.ColumnName);
            if (this.column.Length < int.MaxValue)
                this.EditorControl.Rows = (int)Math.Ceiling(this.column.Length / 100d);

            ClientScriptManager jsManager = Page.ClientScript;
            if (!jsManager.IsClientScriptIncludeRegistered("tinymce-nemo"))
                jsManager.RegisterClientScriptInclude("tinymce-nemo", ConfigurationManager.AppSettings["TinyMCE:BasePath"].ReplaceEx("~/", "/") + "tiny_mce.js");
            if (!jsManager.IsClientScriptBlockRegistered("nemo-serv-fb"))
                jsManager.RegisterClientScriptBlock(typeof(HtmlEditControl), "nemo-serv-b", "<script type=\"text/javascript\">var serviceUrl = '" + HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/Dialog/FileDialog.aspx';</script>");
            if (!jsManager.IsClientScriptIncludeRegistered("tinymce-nemo-sup"))
                jsManager.RegisterClientScriptInclude("tinymce-nemo-sup", NemoUrlUtils.GetStartUrl() + "Controls/HtmlEditControl.js");
            jsManager.RegisterClientScriptBlock(typeof(HtmlEditControl), "nemo-serv-b-" + column.ColumnId, "<script type=\"text/javascript\">TinyMCESetupEditor('" + HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "', '" + this.EditorControl.ClientID + "', '" + Guid.NewGuid() + "', '" + site.Skin + "', '" + String.Join(";", bContentStyles.ToArray()) + "');</script>");
        }

        private void PopulateControlCKEditor() {
            SiteSettings site = CacheHelper.GetCurrentSiteSettings();
            //List<ContentStyle> contentStyles = ContentStyle.GetAll(site.SiteGuid);
            //List<string> bContentStyles = new List<string>();
            //foreach (ContentStyle contentStyle in contentStyles.Where(cs => cs.IsActive)) {
            //    bContentStyles.Add(contentStyle.Name + "=" + contentStyle.CssClass);
            //}

            // Dodanie stylu z strony do edytora
            //if (Page.Header.FindControl("mojoStyle") == null) {
            //    Literal lStyle = new Literal();
            //    lStyle.Text = "<link rel='stylesheet' type='text/css' href='/csshandler.ashx?skin=" + site.Skin + "&sv=" + Guid.NewGuid() + "' />";
            //    lStyle.ID = "mojoStyle";
            //    Page.Header.Controls.Add(lStyle);
            //}

            this.EditorControl = new TextBox();
            this.EditorControl.ID = "EditControl" + column.ColumnId;
            this.Controls.Add(this.EditorControl);
            this.EditorControl.TextMode = TextBoxMode.MultiLine;
            this.EditorControl.Text = this.record.GetString(this.column.ColumnName);
            if (this.column.Length < int.MaxValue)
                this.EditorControl.Rows = (int)Math.Ceiling(this.column.Length / 100d);

            ClientScriptManager jsManager = Page.ClientScript;
            if (!jsManager.IsClientScriptIncludeRegistered("ckeditor-nemo"))
                jsManager.RegisterClientScriptInclude("ckeditor-nemo", ConfigurationManager.AppSettings["CKEditor:BasePath"].ReplaceEx("~/", "/") + "ckeditor.js");
            if (!jsManager.IsClientScriptIncludeRegistered("tinymce-nemo-sup"))
                jsManager.RegisterClientScriptInclude("tinymce-nemo-sup", "/Nemo/Controls/HtmlEditControl.js");
            jsManager.RegisterClientScriptBlock(typeof(HtmlEditControl), "ckeditor-init-" + column.ColumnId, "<script type=\"text/javascript\">$(function(){ CKEditorSetupEditor('" + this.EditorControl.ClientID + "', '" + Guid.NewGuid() + "', '" + site.Skin + "', '" + ConfigurationManager.AppSettings["CKEditor:ConfigPath"].ReplaceEx("~/", "/") + "', '" + ConfigurationManager.AppSettings["CKEditor:Skin"] + "');});</script>");
        }

        public override void ReSetValue(object value) {
            this.EditorControl.Text = value.ToString();
        }

        public override void SaveInRecord() {
            this.record.SetValue(this.column.ColumnName, this.Page.Request[this.EditorControl.UniqueID]);
        }

        public override string GetValue() {
            return this.Page.Request[this.EditorControl.UniqueID];
        }

    }
}